package com.flyco.modules.returns.service.impl;

import com.flyco.common.util.IdGenerator16Bit;
import com.flyco.common.util.IdUtil;
import com.flyco.modules.returns.entity.OmsOrderReturnApply;
import com.flyco.modules.returns.entity.OmsOrderReturnApplyOms;
import com.flyco.modules.returns.entity.OmsOrderReturnItem;
import com.flyco.modules.returns.entity.OmsOrderReturnItemBatch;
import com.flyco.modules.returns.mapper.OmsOrderReturnApplyOmsMapper;
import com.flyco.modules.returns.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 退货申请中间表
 * @Author: flyco
 * @Date: 2023-08-25
 * @Version: V1.0
 */
@Service
public class OmsOrderReturnApplyOmsServiceImpl extends ServiceImpl<OmsOrderReturnApplyOmsMapper, OmsOrderReturnApplyOms> implements IOmsOrderReturnApplyOmsService {

    @Autowired
    private OmsOrderReturnApplyOmsMapper returnApplyOmsMapper;
    @Autowired
    private IOmsOrderReturnApplyService returnApplyService;
    @Autowired
    private IOmsOrderReturnItemService returnItemService;
    @Autowired
    private IOmsOrderReturnItemOmsService returnItemOmsService;
    @Autowired
    private IOmsOrderReturnItemBatchService returnItemBatchService;

    @Override
    public void savaByApplyId(Long applyId) {

        List<OmsOrderReturnItemBatch> returnItemList = returnItemBatchService.getByApplyId(applyId);

        List<OmsOrderReturnItemBatch> useLessList = returnItemList.stream().filter(o -> o.getUselessUnitQuantity() > 0).collect(Collectors.toList());
        List<OmsOrderReturnItemBatch> reuseList = returnItemList.stream().filter(o -> o.getReuseUnitQuantity() > 0).collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(useLessList)) {
            //创建报废申请
            OmsOrderReturnApplyOms returnApplyOms = saveApply(applyId, "useLess");
            returnItemOmsService.saveUseLessByItems(useLessList, returnApplyOms.getId());
        }
        if (!CollectionUtils.isEmpty(reuseList)) {
            //创建返仓申请
            OmsOrderReturnApplyOms returnApplyOms = saveApply(applyId, "reUse");
            returnItemOmsService.saveReUseByItems(reuseList, returnApplyOms.getId());
        }
    }

    //type:useLess,reUse
    public OmsOrderReturnApplyOms saveApply(Long applyId, String type) {

        OmsOrderReturnApply returnApply = returnApplyService.getById(applyId);

        OmsOrderReturnApplyOms returnApplyOms = new OmsOrderReturnApplyOms();
        BeanUtils.copyProperties(returnApply, returnApplyOms);
        returnApplyOms.setReturnApplyId(returnApply.getId());

        if ("useLess".equals(type)) {
            returnApplyOms.setId(Long.valueOf(returnApply.getId().toString() + "01"));
            returnApplyOms.setReturnType("JXBF");
        } else {
            returnApplyOms.setId(Long.valueOf(returnApply.getId().toString() + "02"));
            returnApplyOms.setReturnType("JXFC");
        }
        save(returnApplyOms);

        return returnApplyOms;
    }
}
